package com.lzq.rabbitmqdemo.java_api.direct;

import com.lzq.rabbitmqdemo.java_api.constant.ConnectionConfig;
import com.lzq.rabbitmqdemo.java_api.constant.Constants;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class DirectRabbitProducer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = ConnectionConfig.getFactory();
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //声明交换机
            channel.exchangeDeclare(Constants.DIRECT_EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, false, null);
            //声明队列
            channel.queueDeclare(Constants.DIRECT_QUEUE_NAME1,true, false, false, null);
            channel.queueDeclare(Constants.DIRECT_QUEUE_NAME2,true, false, false, null);
            //绑定交换机和队列
            channel.queueBind(Constants.DIRECT_QUEUE_NAME1,Constants.DIRECT_EXCHANGE_NAME,"orange");
            channel.queueBind(Constants.DIRECT_QUEUE_NAME2,Constants.DIRECT_EXCHANGE_NAME,"black");
            String msg1 = "hello orange";
            channel.basicPublish(Constants.DIRECT_EXCHANGE_NAME,"orange",null,msg1.getBytes());
            String msg2 = "hello black";
            channel.basicPublish(Constants.DIRECT_EXCHANGE_NAME,"black",null,msg2.getBytes());
        }
    }
}
